# -*- coding: UTF-8 -*-
from django.forms import ModelForm
from .models import Server,ServerHardInfo
from django import forms
from myauth.models import PlatForm
#定义Node的Form,Form名字为 模式名+Form
class ServerForm(ModelForm):
    #自定义ModelForm的内容
    def clean_ip(self):
        ip = self.cleaned_data['ip']
        if 'ip' in self.changed_data:

            ips=Server.objects.filter(ip=ip)
            if ips:
                raise forms.ValidationError('IP:%s已经存在' %ip)
        return ip

    def clean_ip_extra(self):
        ip_extra = self.cleaned_data['ip_extra']
        if 'ip_extra' in self.changed_data:

            ip_extras = Server.objects.filter(ip_extra=ip_extra)
            if ip_extras:
                raise forms.ValidationError('ip_extra:%s已经存在' % ip_extra)
        return ip_extra




    class Meta:
        #该ModelForm参照Model: Node
        model = Server
        #在Form中不显示node_signer这个字段
        # exclude = ['node_signer']
        #fields=['id','ip','ip_extra']
        exclude=[]


class Server_Search_Form(forms.Form):
    ip=forms.CharField(label='缺省IP:',required=False)
    machine_room=forms.CharField(label='机房:',required=False)
    purpose=forms.CharField(label='用途:',required=False)
    platform=forms.ModelChoiceField(
                label='平台:',queryset=PlatForm.objects.all(),to_field_name='id',required=False)


class ServerHardInfoForm(ModelForm):
    class Meta:
        model= ServerHardInfo
        exclude=[]


class ServerHardInfo_SearchForm(ModelForm):
    class Meta:
        model= ServerHardInfo
        exclude=['all_info','product','hostname']
    #ip=forms.CharField(label='缺省IP:',required=False)

